package tech.aistar.service.impl.notecom;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.entity.notecom.Com;
import tech.aistar.entity.notecom.ComVo;
import tech.aistar.mapper.notecom.ComMapper;
import tech.aistar.service.authordis.INoteTravelerDisService;
import tech.aistar.service.notecom.IComService;
import tech.aistar.service.notecom.IComVoService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/10/9.
 */
@Service
@Transactional
public class ComVoServiceImpl implements IComVoService {
    @Autowired
    private ComMapper comMapper;
    @Autowired
    private IComService comService;
//    @Autowired
//    private IUserInfoService userInfoService;
    @Autowired
    private INoteTravelerDisService noteTravelerDisService;

    /**
     * 根据帖子id处查询所有的评论
     * 先将所有评论取出
     */
    @Override
    public List<ComVo> findAllByNoteId(Integer noteId) {
        List<Com> comList1 = comService.findOneComment(noteId);

        //将上面的一级评论放入新建comVoList集合中
        List<ComVo> comVoList = new ArrayList<>();
        comList1.forEach(System.err::println);

        //遍历一级评论
        for (Com c:comList1){
            ComVo comVo = new ComVo();
            comVo.setComId(c.getComId());
            comVo.setFloor(c.getFloor());//楼层
            comVo.setNoteId(c.getNoteId());
            comVo.setUserId(c.getUserId());
            comVo.setToUserId(c.getToUserId());
            comVo.setContent(c.getContent());
            comVo.setCtime(c.getCtime());
            comVo.setPointNum(c.getPointNum());
            comVo.setFlag(c.getFlag());

            //遍历一级子评论
            List<Com> comList2 = comService.findAllTwoComment(c.getFloor(),c.getNoteId());
            System.err.println("--------4444-------------");
            System.err.println(comList2+"11111111111111111111111111111");
            System.err.println("--------44444------------");
            for (Com r:comList2){
//                r.setUserTable(userInfoServic.findById(r.getUserId()));
                r.setUserTable(noteTravelerDisService.findUserById(r.getUserId()));
                r.setToUserTable(noteTravelerDisService.findUserById(r.getToUserId()));
//                r.setToUserTable(userInfoService.findById(r.getToUserId()));
            }
            if (comList2 != null){
                comVo.setTwocomList(comList2);
            }
//            comVo.setUserInfo(userInfoService.findById(c.getUserId()));
//            comVo.setToUserInfo(userInfoService.findById(c.getToUserId()));
            comVo.setUserTable(noteTravelerDisService.findUserById(comVo.getUserId()));
            comVo.setToUserTable(noteTravelerDisService.findUserById(comVo.getToUserId()));

            comVoList.add(comVo);
        }
        return comVoList;
    }
}
